package com.jwx.mycar.controller;

import com.jwx.mycar.entity.po.User;
import com.jwx.mycar.entity.vo.ResultVo;
import com.jwx.mycar.service.PictureService;
import com.jwx.mycar.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: jwx
 * @time: 2019/11/27 21:11
 */
@Api(tags = "图片相关")
@RestController
@RequestMapping("/picture")
public class PictureController {
    @Autowired
    PictureService pictureService;
    @Autowired
    ResultVOUtil resultVOUtil;
    @ApiOperation("获取用户上传的图片总数")
    @GetMapping("/getPicCount")
    public ResultVo getPicCount(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
         String userId = user.getUserId();
        int count =  pictureService.getPicCount(userId);
        return resultVOUtil.success(count);
    }
    /**
     * 点赞图片
     */
    @ApiOperation(value = "点赞", notes = "传picId")
    @ApiImplicitParam(name = "picId", value = "picId", required = true, dataType = "String", paramType = "query")
    @PostMapping("/up")
    public ResultVo upFile(String picId, HttpServletRequest request) {

        if (picId == null) return resultVOUtil.fail("图片id不能为空");
        User user = (User)request.getSession().getAttribute("user");
        int count = pictureService.upFile(picId, user.getUserId());
        return resultVOUtil.success(count);
    }
    @ApiOperation(value = "获取点赞数")
    @ApiImplicitParam(name = "picId", value = "图片id",dataType = "String",required = true)
    @PostMapping("/getupById")
    public ResultVo getUpBy(@RequestParam("picId") String picId){
       int upCount =  pictureService.getUpById(picId);
       return resultVOUtil.success(upCount);
    }
}

